/*
 * @Date: 2025-01-06 14:37:19
 * @Author: LiSong
 * @LastEditors: LiSong
 * @LastEditTime: 2025-01-08 09:35:57
 * @Description: SM4加解密
 */
import { SM4 } from 'gm-crypto'

function stringToHex(str){
	let hex = Array.prototype.map.call(str, (c) => {
		return c.charCodeAt(0).toString(16);
	}).join("");
	return hex;
}
/**
 * SM4加解密工具类
 */
const SM4Utils = {
  // key
  key: stringToHex('11HDESaAhiHHugDz'),

  /**
   * 加密方法
   * 
   * @param {String} plainText 需要加密的内容
   * @returns 
   */
  encrypt (plainText) {
    return SM4.encrypt(plainText, this.key, {
      inputEncoding: 'utf8',
      outputEncoding: 'base64'
    })
  },
  /**
   * 解密方法
   * 
   * @param {String} cipherText 需要解密的内容
   * @returns 
   */
  decrypt (cipherText) {
    return SM4.decrypt(cipherText, this.key, {
      inputEncoding: 'base64',
      outputEncoding: 'utf8'
    })
  }
}

export { SM4Utils }

export default SM4Utils
